package h.d.bluetooth.visitor.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.os.Bundle;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import h.d.bluetooth.visitor.db.BVDBHelper;
import h.d.bluetooth.visitor.db.DBControl;
import h.d.bluetooth.visitor.sort.SortData;


public class VaE {
	/**
	 * @author hdeom
	 * @date 2014. 1. 14.
	 *
	 */

	//로그제어
	public static boolean HD = true;

	public static VaE VaEManager;



	public final static int REQUEST_ENABLE_BT = 20;

	public final static int ADD_BLUETOOTH_DEVICE = 1000;
	public final static int FINISHI_BLUETOOTH_SCAN = 1001;

	public static int POPUP_MODE = 1;

	public final static int POPUP_DEVICE_INFO = 30;
	public final static int POPUP_ADD_DEVICE = 31;

	public final static String DBFILENAME = "BluetoothVisitor.db";
	public final static int DBVERSION = 1;

	public static BVDBHelper mInstance = null;

	public static DBControl DBCONTROL;

	public static ArrayList<SortData> AddedVisitor;
	public static ArrayList<SortData> FoundDevice;


	public static Calendar Clock;
	public static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");








	/**
	 * @author hdeom
	 * @date 2014. 1. 15.
	 * @param
	 * @return
	 *
	 **/
	public void addList(SortData s) {

		for(int i = 0; i<VaE.FoundDevice.size(); i++){
			SortData sort = VaE.FoundDevice.get(i);

			if(TextUtils.equals(sort.getMacAddress(), s.getMacAddress())){
				Clock = Calendar.getInstance();
				String date = yyyyMMdd.format(Clock.getTime());
				s.setTime(date);

				if(sort.getRssi() != s.getRssi()){
					VaE.FoundDevice.set(i, s);
					DBCONTROL.insertDeviceLog(s);
					return;
				}else{

					DBCONTROL.insertDeviceLog(s);
					return;
				}
			}
		}

		VaE.FoundDevice.add(s);

	}

	public String makeDeviceText(SortData s){

		String re = "";

		for(SortData sAll : VaE.AddedVisitor){
			if(TextUtils.equals(s.getMacAddress(), sAll.getMacAddress())){
				s.setFixName(sAll.getFixName());
			}
		}

		re = s.getName()+" ("+s.getFixName()+")\n";
		re = re+s.getMacAddress() + "  거리 : "+s.getRssi();

		return re;
	}
}
